In [1]:
import os
In [2]:
os.path.join('usr', 'bin', 'spam')
Out[2]:
In [3]:
#The Current Working Directory
os.getcwd()
Out[3]:
In [14]:
#Move to desktop
os.chdir(os.path.join('/','home','sudhanshu','Desktop'))
os.getcwd()
Out[14]:
In [5]:
#ABasolute path
os.path.abspath('.')
#return absolute path of current working directory becouse '.' specify current working directory
Out[5]:
In [7]:
os.path.abspath('../') #return absolute address of relative directory '../'
Out[7]:
In [12]:
#check given address is absolute or not
print os.path.isabs('/home/sudhanshu/GitHub/Fun-Tool')
print os.path.isabs('../')
print os.path.isabs(os.path.abspath('../'))
print os.path.isabs(os.getcwd())
In [15]:
print os.path.relpath('/home/sudhanshu/', '/home/sudhanshu/GitHub/Fun-Tool')
print os.path.relpath('/home/sudhanshu/GitHub/Fun-Tool','/home/sudhanshu/')
In [16]:
path = 'home/sudhanshu/GitHub/Fun-Tool/readme.md'
print os.path.basename(path)
print os.path.dirname(path)
Using Split function
In [17]:
print os.path.split(path)
In [19]:
#Split into each sublocation
path.split(os.path.sep)
Out[19]:
In [35]:
#calculate size of a file
path='/home/sudhanshu/Downloads/dropbox_2015.10.28_amd64.deb'
print os.path.getsize(path)/float(1024),'KB'
In [2]:
#calculate size of a folder
#first find all file inside that folder
fileList=os.listdir('/home/sudhanshu/Downloads')
print fileList
print 'Sum of size of each file gives, size of folder'
folderSize=0
for filename in fileList:
folderSize+=os.path.getsize(os.path.join('/home/sudhanshu/Downloads/',filename))
print 'folder Size of /home/sudhanshu/Downloads is:',float(folderSize)/(1024*1024),'MB'
Calling os.path.exists(path) will return True if the file or folder referred
to in the argument exists and will return False if it does not exist.
Calling os.path.isfile(path) will return True if the path argument exists
and is a file and will return False otherwise.
Calling os.path.isdir(path) will return True if the path argument exists
and is a folder and will return False otherwise.
In [9]:
print os.path.exists('/home/sudhanshu')
print os.path.exists('/home/sudhanshu/Downloads/dropbox_2015.10.28_amd64.deb')
In [11]:
print os.path.isdir('/home/sudhanshu/Downloads/dropbox_2015.10.28_amd64.deb') # return false becouse its a file not directory
print os.path.isdir('/home/sudhanshu/Downloads')
In [12]:
print os.path.isfile('/home/sudhanshu/Downloads/dropbox_2015.10.28_amd64.deb')
print os.path.isfile('/home/sudhanshu/Downloads')
In [ ]:
f = open('filename.extention','r')
#read entire file at a time
fileContent=f.read()
#read each line
lst=f.readlines()
f.close()
The shelve module will let you add Save and Open features to your program. For example, if you ran a program and entered some configuration settings, you could save those settings to a shelf file and then have the program load them the next time it is run.
In [2]:
import shelve
In [13]:
#writing data
shelfFile = shelve.open('mydata')
cats = ['Zophie', 'kitty', 'Simon']
cost=[34435,1332,3452]
shelfFile['cats']=cats #Stores cat var in file
shelfFile['cost']=cost
shelfFile.close() #close file
In [14]:
#reading data
shelfFile = shelve.open('mydata')
print type(shelfFile)
print shelfFile['cats'] #read variables
print shelfFile['cost']
In [17]:
#return keys
print list(shelfFile.keys())
#return values
print list(shelfFile.values())
In [19]:
import pprint
#deals with json formate
In [20]:
cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka', 'desc': 'fluffy'}]
pprint.pformat(cats)
Out[20]:
In [21]:
#Writing data
f = open('myCats.py', 'w')
f.write('cats = ' + pprint.pformat(cats) + '\n')
#it simply create a python file
f.close()
In [22]:
# Now import Python file & use
import myCats
myCats.cats
Out[22]:
In [ ]: